/**
 * 设备信息表管理初始化
 */
var EquipmentInfo = {
    id: "EquipmentInfoTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
EquipmentInfo.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'ID', field: 'id', visible: true, align: 'center', valign: 'middle'},
        {title: '设备名称', field: 'equipmentName', visible: true, align: 'center', valign: 'middle'},
        {title: '通信方式', field: 'communicationName', visible: true, align: 'center', valign: 'middle'},
        {title: 'IP地址', field: 'ipAddress', visible: true, align: 'center', valign: 'middle'},
        {title: '通信端口', field: 'communicationPost', visible: true, align: 'center', valign: 'middle'},
        {title: '通信密码', field: 'communicationPassword', visible: true, align: 'center', valign: 'middle'},
        {title: '图标类型', field: 'iconType', visible: true, align: 'center', valign: 'middle'},
        {title: '门禁控制器类型', field: 'controllerName', visible: true, align: 'center', valign: 'middle'},
        {title: '区域', field: 'areaName', visible: true, align: 'center', valign: 'middle'},
        {title: '序列号', field: 'serialNumber', visible: true, align: 'center', valign: 'middle'},
    ];
};

/**
 * 检查是否选中
 */
EquipmentInfo.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        EquipmentInfo.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加设备信息表
 */
EquipmentInfo.openAddEquipmentInfo = function () {
    var index = layer.open({
        type: 2,
        title: '添加设备信息表',
        area: ['85%', '85%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/door/EQUIPMENT/INFO/add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看设备信息表详情
 */
EquipmentInfo.openEquipmentInfoDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '系统参数详情',
            area: ['85%', '85%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/door/EQUIPMENT/INFO/edit/' + EquipmentInfo.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除设备信息表
 */
EquipmentInfo.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/door/EQUIPMENT/INFO/delete", function (data) {
            Feng.success("删除成功!");
            EquipmentInfo.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询设备信息表列表
 */
EquipmentInfo.search = function () {
    var queryData = {};
    queryData['equipmentName'] = $("#equipmentName").val();
    EquipmentInfo.table.refresh({query: queryData});
};
/**
 * 重置查询条件
 */
EquipmentInfo.reset = function () {
    $('#equipmentName').val('');
    this.search();
};

$(function () {
    var defaultColunms = EquipmentInfo.initColumn();
    var table = new BSTable(EquipmentInfo.id, "/door/EQUIPMENT/INFO/list", defaultColunms);
    table.setPaginationType("server");
    EquipmentInfo.table = table.init();
});

/**
 * 同步设备时间
 */
EquipmentInfo.setDateTime = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/door/EQUIPMENT/INFO/setDateTime", function (data) {
            Feng.success("删除成功!");
            EquipmentInfo.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",this.seItem.id);
        ajax.start();
    }
};
/**
 * 同步所有数据到设备
 */
EquipmentInfo.setDataToDevice = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/door/EQUIPMENT/INFO/setDataToDevice", function (data) {
            Feng.success(data.message);
            EquipmentInfo.table.refresh();
        }, function (data) {
            Feng.error("同步失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",this.seItem.id);
        ajax.start();
    }
};
/**
 * 获取事件记录
 */
EquipmentInfo.getRecord = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/door/EQUIPMENT/INFO/getRecord", function (data) {
            Feng.success("获取事件记录成功!");
            EquipmentInfo.table.refresh();
        }, function (data) {
            Feng.error("获取事件记录失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",this.seItem.id);
        ajax.start();
    }
};
/**
 * 获取人员信息
 */
EquipmentInfo.openUserForDevice = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '系统参数详情',
            area: ['85%', '85%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/door/EQUIPMENT/INFO/getUserForDevice/' + EquipmentInfo.seItem.id
        });
        this.layerIndex = index;
    }
};

EquipmentInfo.areaEdit = function () {
        var index = layer.open({
            type: 2,
            title: '编辑区域',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/dict/area_dict_edit/'
        });
        this.layerIndex = index;
};